﻿using System;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using NAGFwk.BL;
using NAGFwk.UI;
using SilkeyERP.BL;
using SilkeyERP.UI.Entities.Customers;

namespace SilkeyERP.UI.Entities.Customer
{
    public partial class ContentView : BaseContentDetail
    {
        private IBaseController<Model.Customer> _controller = new CustomerController();
        public override string ModuleName { get { return "Customers"; } }

        public ContentView()
        {
            InitializeComponent();
        }

        public override void ShowModule(bool firstShow)
        {
            base.ShowModule(firstShow);
            MapItLoad(_controller);
            this.gvBaseContentDetail.OptionsEditForm.CustomEditFormLayout = new CustomEditContentView();
            this.OnUpdateGrid += ContentView_OnUpdateGrid;
        }

        void ContentView_OnUpdateGrid(object m, RowObjectEventArgs e)
        {
            MapItUpdate(e, _controller);
        }

        private void ContentView_Load(object sender, EventArgs e)
        {
            cityBindingSource.DataSource = new CityController().GetAll();
            customerTypeBindingSource.DataSource = new CustomerTypeController().GetAll();
            paymentMethodBindingSource.DataSource = new PaymentMethodController().GetAll();
            priceListBindingSource.DataSource = new SalesPriceListController().GetAll();
            salesPersonBindingSource.DataSource = new SalesPersonController().GetAll();
            taxFigureBindingSource.DataSource = new  TaxFigureController().GetAll();
            provinceBindingSource.DataSource = new ProvinceController().GetAll();
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            MapItDelete(_controller);
        }

      
        private void bbiRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshAll();
        }

        private void gvBaseContentDetail_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            ValidateNullFields(e);
        }

        private void RefreshAll()
        {
            _controller = MapItRefresh(new CustomerController());
        }

        private void ValidateNullFields(ValidateRowEventArgs e)
        {
            var customer = (Model.Customer)e.Row;

            if (string.IsNullOrEmpty(customer.Name)) { e.ErrorText = "Nombre de Cliente es Requerido."; e.Valid = false; }
            if (customer.TaxFigureID == int.MinValue) { e.ErrorText = "Figura Fiscal es Requerido."; e.Valid = false; }
            if (customer.CustomerCode == int.MinValue) { e.ErrorText = "Codigo de Cliente es Requerido."; e.Valid = false; }
            if (customer.CustomerTypeID == int.MinValue) { e.ErrorText = "Tipo de Cliente es Requerido."; e.Valid = false; }
            //if (customer.CustomerBalance == 0) { e.ErrorText = "CustomerBalance Is Required."; e.Valid = false; }
            if (customer.SalesPriceListID == int.MinValue) { e.ErrorText = "Lista de Precio de Venta es Requerido."; e.Valid = false; }
            if (customer.SalesPersonID == int.MinValue) { e.ErrorText = "Vendedor es Requerido."; e.Valid = false; }
        }

        private void gvBaseContentDetail_EditFormShowing(object sender, EditFormShowingEventArgs e)
        {
            var view = sender as GridView;
            var customEditForm = view.OptionsEditForm.CustomEditFormLayout as CustomEditContentView;
            var myCurrent = (Model.Customer)bsBaseContentDetail.Current;
            if (customEditForm != null)
                customEditForm.InitData(ref myCurrent);
        }
      
    }
}
